home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / stdio / c / tmpfile < prev    next >
Text File  |  1996-11-09  |  2KB  |  98 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/stdio/c/RCS/tmpfile,v $
  4.  * $Date: 1996/05/06 09:01:34 $
  5.  * $Revision: 1.2 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: tmpfile,v $
  10.  * Revision 1.2  1996/05/06 09:01:34  unixlib
  11.  * Updates to sources made by Nick Burrett, Peter Burwood and Simon Callan.
  12.  * Saved for 3.7a release.
  13.  *
  14.  * Revision 1.1  1996/04/19 21:32:42  simon
  15.  * Initial revision
  16.  *
  17.  ***************************************************************************/
  18.  
  19. static const char rcs_id[] = "$Id: tmpfile,v 1.2 1996/05/06 09:01:34 unixlib Rel $";
  20.  
  21. #include <string.h>
  22. #include <stdio.h>
  23.  
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26.  
  27. __STDIOLIB__
  28.  
  29. #define MKTEMP
  30.  
  31. FILE *
  32. tmpfile (void)
  33. {
  34.   if (__tmpf)
  35.     return (0);
  36.  
  37.   tmpnam (__tmpn);
  38.   return (__tmpf = fopen (__tmpn, "wb+"));
  39. }
  40.  
  41. static char *
  42. __tmpnam (register char *buf, register char *dir,
  43.       register char *template)
  44. {
  45.   register char *s = buf;
  46.   register int i, j = 0;
  47.  
  48.   if (dir)
  49.     {
  50.       while (*s++ = *dir++);
  51.       s--;
  52.       *s++ = '/';
  53.     }
  54.   while (*s++ = *template++);
  55.   s -= 7;
  56.   if (*s != 'X')
  57.     return (0);
  58.  
  59. loop:i = __tmpcnt++, j++;
  60.   sprintf (s, "%.6x", i);
  61.  
  62.   if (!access (buf, 0))
  63.     {
  64.       if ((__tmpcnt += 0x131) >= 0x1000000)
  65.     __tmpcnt &= 0x3ff;
  66.       if (j > 256)
  67.     return (0);        /* max. 256 tries */
  68.       goto loop;
  69.     }
  70.  
  71.   return (buf);
  72. }
  73.  
  74. static char __tmpbuf[L_tmpnam + 1];
  75.  
  76. char *
  77. tmpnam (register char *buf)
  78. {
  79.   if (!buf)
  80.     buf = __tmpbuf;
  81.  
  82.   return (__tmpnam (buf, P_tmpdir, "__XXXXXX"));
  83. }
  84.  
  85. #ifdef MKTEMP
  86. char *
  87. mktemp (register char *template)
  88. {
  89.   return (__tmpnam (template, 0, template));
  90. }
  91.  
  92. int
  93. mkstemp (char *template)
  94. {
  95.   return (open (mktemp (template), O_RDWR | O_CREAT | O_TRUNC, 0666));
  96. }
  97. #endif
  98.